package com.cskaoyan.mall.service;

import com.cskaoyan.mall.bean.Coupon;
import com.cskaoyan.mall.bean.CouponExample;
import com.cskaoyan.mall.bean.CouponUser;
import com.cskaoyan.mall.bean.CouponUserExample;
import com.cskaoyan.mall.bean.bo.CouponBo;
import com.cskaoyan.mall.mapper.CouponMapper;
import com.cskaoyan.mall.mapper.CouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;


    @Override
    public List<Coupon> queryCouponList(Integer page, Integer limit, String name, Short type, Short status, String sort, String order) {
        PageHelper.startPage(page,limit);
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        if (name != null){
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null){
            criteria.andTypeEqualTo(type);
        }
        if (status != null){
            criteria.andStatusEqualTo(status);
        }
        couponExample.setOrderByClause(sort + " " + order);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        PageInfo<Coupon> couponPageInfo = new PageInfo<Coupon>(coupons);
        return coupons;
    }
    @Override
    public HashMap<String,Object> queryCouponUserList(Integer page, Integer limit, Integer couponId, String sort, String order) {
        PageHelper.startPage(page,limit);
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();
        criteria.andCouponIdEqualTo(couponId);
        couponUserExample.setOrderByClause(sort + " " + order);
        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);
        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>();
        int total = (int) couponUserPageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("items",couponUsers);
        return map;
    }

    @Override
    public Coupon createCoupon(Coupon coupon) {
        int insert = couponMapper.insertByCoupon(coupon);
        Coupon couponVo = couponMapper.selectLastInsertCoupon();
        return couponVo;
    }

    @Override
    public Coupon updateCoupon(Coupon coupon) {
        int i = couponMapper.updateByPrimaryKey(coupon);
        return couponMapper.selectByPrimaryKey(coupon.getId());
    }

    @Override
    public Coupon queryCouponById(Integer id) {
        return couponMapper.selectByPrimaryKey(id);
    }

    @Override
    public void deleteCoupon(Coupon coupon) {
        couponMapper.deleteCouponById(coupon.getId());

    }


}
